import { ElMessage, ElMessageBox,type ElMessageBoxOptions } from 'element-plus'
import type { AppContext, VNode } from 'vue'
import 'element-plus/es/components/message/style/css'
import 'element-plus/es/components/message-box/style/css'

interface MessageImplements {
  info(title: string): void;

  warning(title: string): void;

  success(title: string): void;

  error(title: string): void;
}

export function useMessage() {
  class MessageClass implements MessageImplements {
    // 普通提示
    info(title: string): void {
      ElMessage.info(title);
    }

    // 警告提示
    warning(title: string): void {
      ElMessage.warning(title);
    }

    // 成功提示
    success(title: string): void {
      ElMessage.success(title);
    }

    // 错误提示
    error(title: string): void {
      ElMessage.error(title);
    }
  }

  return new MessageClass();
}

export function useMessageBox(appContext?:AppContext) {
  class MessageBoxClass implements MessageImplements {
    // 普通提示
    info(msg: string): void {
      ElMessageBox.alert(msg, '提示信息');
    }

    // 警告提示
    warning(msg: string): void {
      ElMessageBox.alert(msg, '提示信息', {type: 'warning'});
    }

    // 成功提示
    success(msg: string): void {
      ElMessageBox.alert(msg, '提示信息', {type: 'success'});
    }

    // 错误提示
    error(msg: string): void {
      ElMessageBox.alert(msg, '提示信息', {type: 'error'});
    }

    // 确认窗体
    confirm(msg: string, title?: string) {
      if (title == '') {
        title = '提示信息'
      }
      return ElMessageBox.confirm(msg, title, {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      });
    }

    // 提交内容
    prompt(msg: string) {
      return ElMessageBox.prompt(msg, '提示信息', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      });
    }

    custom(title:string|undefined,message:string | VNode | (() => VNode),params?: Exclude<ElMessageBoxOptions, 'title' | 'message'>){
      return ElMessageBox({
        title,
        message,
        ...params
      },appContext)
    }
  }

  return new MessageBoxClass();
}
